<?php 

require(ROOT_PATH.'/include/Action.php');

class Action extends mainAction
{
	public $actionlx	= 'pc';
	public $adminid		= 0;
	public $adminname	= '';
	public $adminface	= '';
	public $adminrs;
	public $admintype	= 0;
	public $loginback	= '';
	public $maxwidth 	= '80%';
	public $deflogin 	= '?m=login';//默认登录页地址
	
	
	public function loginCheck()
	{
		$loginbo  = false;
		if($this->adminid>0){
			$dlur = m('login')->getone("`uid`='$this->adminid'");
			if($dlur){
				$loginbo = true;
			}
		}
		if(!$loginbo){
			if($this->adminid>0)m('login')->exitlogin();
			$url = $this->deflogin;
			if($this->loginback)$url.='&backurl='.$this->jm->base64encode($this->loginback).'';
			if(isajax())$this->showreturn('','登录失效了', 199);
			$this->rock->location($url);
			return;
		}
		$this->getAdmininfo($this->adminid);
	}
	
	public function initProject()
	{
		$this->assign('maxwidth', getconfig('maxwidth', $this->maxwidth));
		$this->adminid		= (int)$this->getsession('adminid',0);
		if($this->adminid==0 && $this->actionlx=='we'){
			$_utesu = $this->rock->cookie('adminid');
			if($_utesu){
				$this->adminid = (int)$this->jm->uncrypt($_utesu);
				$this->rock->savesession(array('adminid' => $this->adminid));
			}
		}
		$this->loginCheck();
	}
	
	public function getAdmininfo($uid)
	{
		$this->adminid  = $uid;
		$this->adminrs	= m('users')->getone("`id`='$uid' and `status`=1");
		if(!$this->adminrs)$this->showreturn('','无效登录了', 199);
		$this->adminrs['face'] = $this->adminface = getface($this->adminrs['face']);
		$this->admintype= $this->adminrs['type'];
		$this->adminname= $this->adminrs['name'];
		$this->rock->adminid 	= $this->adminid;
		$this->rock->adminname 	= $this->adminname;
		$this->assign('adminrs', $this->adminrs);
		$this->updatemysql();
	}
	
	public function returnerror($msg='')
	{
		return returnerror($msg);
	}
	
	public function returndata($data=array())
	{
		return returnsuccess($data);
	}
	
	
	
	public function savecellAction()
	{
		$tab 	= (int)$this->post('tablename','0');
		$id 	= (int)$this->post('id','0');
		$fields = $this->post('fieldname');
		$value 	= $this->post('value');
		$tabla	= array('agent','users','worc','dept','word','file','logintoken','log');
		$table  = arrvalue($tabla, $tab);
		if(isempt($table) || $id<=0)return returnsuccess('没有找到表');
		m($table)->update(array($fields => $value), $id);	
		return returnsuccess();
	}
	
	public function deljiluAction()
	{
		$tab 	= (int)$this->post('tablename','0');
		$id 	= (int)$this->post('id','0');
		$tabla	= array('agent','users','worc','dept','word','file','logintoken','log');
		$table  = arrvalue($tabla, $tab);
		if(isempt($table) || $id<=0)return returnsuccess('没有找到表');
		m($table)->delete($id);	
		return returnsuccess();
	}
	
	public function filejsbase()
	{
		$url = getconfig('rockfile_url');
		if(!$url)return '';
		return '<script src="'.$url.'?m=filejs&key='.getconfig('rockfile_key').'&a=base&optid='.$this->adminid.'&optname='.$this->jm->base64encode($this->adminname).'"></script>'.chr(10).'';
	}
	
	//更新数据库
	private function updatemysql()
	{
		$path = 'res/css/abcziduan.txt';
		if(file_exists($path) && getconfig('systype')!='dev'){
			$sqarr = explode('ROCKSPLIT', file_get_contents($path));
			foreach($sqarr as $_sql1)if($_sql1)$this->db->query($_sql1,false);
			@unlink($path);
		}
	}
}

class ActionNot extends Action
{
	public function loginCheck()
	{
		
	}
}

class ActionWe extends Action
{

	public $actionlx	= 'we';
	public $deflogin 	= '?m=login&d=we';
}

class ActionManage extends Action
{

	public function loginCheck()
	{
		parent::loginCheck();
		if($this->admintype!='1' && $this->admintype!='2')exit('非管理员无法进入管理，<a href="javascript:;" onclick="history.back()">返回</a>');
	}
}